package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AlarmPushDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.MonitorDto;
import com.hualude.roadsign.model.MonitorEntity;
import com.hualude.roadsign.repository.AlarmPushRepository;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.MonitorFaultRepository;
import com.hualude.roadsign.repository.MonitorRepository;
import com.hualude.roadsign.service.MonitorService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/8/8.
 */
@Service
public class MonitorServiceImpl implements MonitorService {
    @Resource
    private MonitorRepository monitorRepository;

    @Resource
    private MonitorFaultRepository monitorFaultRepository;

    @Resource
    private AlarmRepository alarmRepository;

    @Resource
    private AlarmPushRepository alarmPushRepository;

    @Override
    public DTResponseDto getMonitor4DT(DTRequestDto dtRequestDto,
                                       String actorIds,
                                       Integer orgId,
                                       Boolean audit,
                                       String startRoad,
                                       String endRoad,
                                       String roadName,
                                       Integer alarmBinding) throws Exception {
        List<MonitorDto> MonitorDtos = monitorRepository.getMonitor4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        Integer count = monitorRepository.getMonitor4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.draw, count, count, MonitorDtos);
        return dtResponseDto;
    }


    @Override
    @Transactional
    public Integer deleteMonitor(String monitorIds) throws Exception {
        String[] monitorIdArray = monitorIds.split(",");
        for (String monitorId : monitorIdArray) {
            monitorFaultRepository.deleteMonitorFault(Integer.parseInt(monitorId));
            monitorRepository.deleteMonitor(Integer.parseInt(monitorId));
        }
        return monitorIdArray.length;
    }

    @Override
    @Transactional
    public Integer addMonitor(MonitorDto monitorDto) throws Exception {
        monitorDto.setMonitorLongitude(FormatNumber.resize(monitorDto.getMonitorLongitude()));
        monitorDto.setMonitorLatitude(FormatNumber.resize(monitorDto.getMonitorLatitude()));

        Integer devicePathId = monitorRepository.save(monitorDto.toBean()).getMonitorId();
        String deviceId = monitorDto.getMonitorADeviceId();
        if (!deviceId.isEmpty()) {
            alarmRepository.updapteAlarmPath(3, devicePathId, deviceId);
        }

        return devicePathId;
    }

    @Override
    @Transactional
    public Integer updateMonitor(MonitorDto monitorDto) throws Exception {
        return monitorRepository.updateMonitor(
                monitorDto.getMonitorId(),
                monitorDto.getProjectNumber(),
                monitorDto.getMonitorLongitude(),
                monitorDto.getMonitorLatitude(),
                monitorDto.getMonitorProvince(),
                monitorDto.getMonitorCity(),
                monitorDto.getMonitorArea(),
                monitorDto.getMonitorRoadName(),
                monitorDto.getMonitorStartRoad(),
                monitorDto.getMonitorEndRoad(),
                monitorDto.getMonitorDirection(),
                monitorDto.getMonitorPoleShape(),
                monitorDto.getMonitorPoleSize(),
                monitorDto.getMonitorBrand(),
                monitorDto.getMonitorPosition(),
                monitorDto.getMonitorSize(),
                monitorDto.getMonitorPhoto(),
                monitorDto.getMonitorInstallTime(),
                monitorDto.getMonitorInstallOrg(),
                monitorDto.getMonitorPeriod(),
                monitorDto.getMonitorServiceLife(),
                monitorDto.getMonitorRemark(),
                monitorDto.getMonitorCreatorId(),
                monitorDto.getMonitorModifierId(),
                monitorDto.getMonitorOrgId(),
                monitorDto.getMonitorMainOrgId());
    }

    @Override
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        return monitorRepository.getStartRoadByRoadName(roadName, actorIds, orgId);
    }

    @Override
    public List<String> getEndRoadByStartRoad(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        return monitorRepository.getEndRoadByStartRoad(city, monitorRoadName, startRoad, actorIds, orgId);
    }

    @Override
    public List<MonitorDto> getMonitor4APP(String actorIds,
                                           Integer orgId,
                                           Integer mCount,
                                           Integer mSize,
                                           String city,
                                           String startRoad,
                                           String endRoad,
                                           String roadName,
                                           String alarmType) throws Exception {
        return monitorRepository.getMonitor4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName, alarmType);
    }

    @Override
    public MonitorDto getMonitorById(Integer monitorId) throws Exception {
        return monitorRepository.getMonitorById(monitorId).get(0);
    }

    @Override
    public List<String> getCity4Monitor4App(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return monitorRepository.getCity4Monitor4App(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getRoadName4MonitorByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return monitorRepository.getRoadName4MonitorByCity(actorIds, orgId, city, mCount, mSize);
    }

    @Override
    public List<String> getEndRoadByStartRoad4App(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return monitorRepository.getEndRoadByStartRoad4App(city, monitorRoadName, startRoad, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return monitorRepository.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<MonitorDto> getMonitor4Map4App(Integer orgId,
                                               String actorIds,
                                               double neLongitude,
                                               double neLatitude,
                                               double swLongitude,
                                               double swLatitude) throws Exception {
        return monitorRepository.getMonitor4Map4App
                (orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }

    @Override
    @Transactional
    public Integer auditMonitor(String monitorIds) throws Exception {
        String[] monitorIdArray = monitorIds.split(",");
        for (String monitorId : monitorIdArray) {
            monitorRepository.auditMonitor(Integer.parseInt(monitorId));
        }
        return monitorIdArray.length;
    }

    @Override
    @Transactional
    public Integer bindingDevice(String deviceId, Integer monitorId) throws Exception {
        MonitorEntity entity = monitorRepository.findOne(monitorId);
        StringBuffer position = new StringBuffer();
        position.append(entity.getMonitorProvince());
        position.append(entity.getMonitorCity());
        position.append(entity.getMonitorArea());
        position.append(" ");
        position.append(entity.getMonitorRoadName());
        position.append(" ");
        position.append(entity.getMonitorStartRoad());
        position.append("-");
        position.append(entity.getMonitorEndRoad());
        int count1 = monitorRepository.bindingDevice(deviceId, monitorId);
        count1 += alarmRepository.bindingDevice(8, monitorId, position.toString(), deviceId);
        return count1;
    }

    @Override
    @Transactional
    public Integer unbindingDevice(String deviceId, Integer highLeverId) throws Exception {
        int count = monitorRepository.unbindingDevice(highLeverId);
        count += alarmRepository.unbindingDevice(deviceId);
        return count;

    }

    @Override
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) throws Exception {
        List<AlarmPushDto> dtos = alarmPushRepository.getAlarmHistory4DT(dtRequestDto, deviceId, deviceType);
        int count = alarmPushRepository.getAlarmHistoryCount4DT(dtRequestDto, deviceId, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<MonitorDto> getDeviceInfo(Integer deviceId) throws Exception {
        return monitorRepository.getDeviceInfo(deviceId);
    }

}
